package com.higgins.lawapp.client;

import java.util.List;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.higgins.lawapp.client.application.dataModel.LawFirm;
import com.higgins.lawapp.client.application.rpc.LawFirmService;
import com.higgins.lawapp.client.application.rpc.LawFirmServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LawApplications implements EntryPoint {
	private final LawFirmServiceAsync lawFirmService = GWT
											.create(LawFirmService.class);
	private LawFirmTable lawFirmTable = new LawFirmTable();
	private ListBox regionListBox = new ListBox();
	private ListBox areaOfLawListBox = new ListBox();
	private ListBox coveringLetterListBox = new ListBox();
	private final Button publishButton = new Button("Publish");
	private Label errorLabelContainer = new Label();
	
	public void onModuleLoad() {
		setUpMainPage();
		
	}
	
	private void setUpMainPage()	{
		populateCoveringLetterListBox();
		populateRegionListBox();
		RootPanel.get().add(coveringLetterListBox);
		RootPanel.get().add(regionListBox);
		RootPanel.get().add(areaOfLawListBox);
		RootPanel.get().add(lawFirmTable);
		RootPanel.get().add(publishButton);
		RootPanel.get("errorLabelContainer").add(errorLabelContainer);
		addHandlers();
	}
	private void populateRegionListBox()	{
		areaOfLawListBox.clear();
		areaOfLawListBox.addItem("----");
		regionListBox.clear();
		regionListBox.addItem("----");
		lawFirmService.retrieveRegions(
				new AsyncCallback<List<String>>() {
					public void onFailure(Throwable caught) {
						errorLabelContainer.setText("Error Loading Regions: " + caught.toString());
					}
					@Override
					public void onSuccess(List<String> result) {
						for (String region:result)
							regionListBox.addItem(region);
					}
				});
	}
	private void populateCoveringLetterListBox()	{
		lawFirmService.retrieveCoveringLetters(
				new AsyncCallback<List<String>>()	{
					public void onFailure(Throwable caught)	{
						errorLabelContainer.setText("Error Retrieving Covering Letters "+ caught.toString());
					}
					public void onSuccess(List<String> results)	{
						if (results!=null && results.size() > 0)
							lawFirmTable.setCoveringLetter(results.get(0));

						for (String coveringLetter:results)	
							coveringLetterListBox.addItem(coveringLetter);
					}
				});
	}
	private void populateAreaOfLawListBox(String region)	{
		areaOfLawListBox.clear();
		areaOfLawListBox.addItem("----");
		lawFirmService.retrieveAreasOfLaw(region,
				new AsyncCallback<List<String>>() {
					public void onFailure(Throwable caught) {
						errorLabelContainer.setText("Error Loading Areas Of Law: " + caught.toString());
					}
					@Override
					public void onSuccess(List<String> result) {
						for (String region:result)
							areaOfLawListBox.addItem(region);
					}
				});
	}	
	private void populateLawFirmTable(String region, String areaOfLaw)	{
		lawFirmService.retrieveLawFirmDetails(region, areaOfLaw,
				new AsyncCallback<List<LawFirm>>() {
					public void onFailure(Throwable caught) {
						errorLabelContainer.setText("Error Loading Law Firms: " + caught.toString());
					}
					@Override
					public void onSuccess(List<LawFirm> result) {
						lawFirmTable.setLawFirms(result);
//						errorLabelContainer.setText(result.get(0) + ", " + result.get(1));
					}
				});
	}
	private void addHandlers()	{
		coveringLetterListBox.addChangeHandler(new ChangeHandler() 	{
			@Override
			public void onChange(ChangeEvent event) {
				lawFirmTable.setCoveringLetter(coveringLetterListBox.getItemText(coveringLetterListBox.getSelectedIndex()));
				String region = regionListBox.getItemText(regionListBox.getSelectedIndex());
				String areaOfLaw = areaOfLawListBox.getItemText(areaOfLawListBox.getSelectedIndex());
				lawFirmService.retrieveLawFirmDetails(region, areaOfLaw,
						new AsyncCallback<List<LawFirm>>() {
							public void onFailure(Throwable caught) {
								errorLabelContainer.setText("Error Loading Law Firms: " + caught.toString());
							}
							@Override
							public void onSuccess(List<LawFirm> result) {
								lawFirmTable.setLawFirms(result);
//								errorLabelContainer.setText(result.get(0) + ", " + result.get(1));
							}
						});				
			}
			
		});
		regionListBox.addChangeHandler(new ChangeHandler()	{
			@Override
			public void onChange(ChangeEvent event) {
				populateAreaOfLawListBox(regionListBox.getItemText(regionListBox.getSelectedIndex()));
			}
		});
		areaOfLawListBox.addChangeHandler(new ChangeHandler()	{
			@Override
			public void onChange(ChangeEvent event) {
				populateLawFirmTable(regionListBox.getItemText(regionListBox.getSelectedIndex()), 
						areaOfLawListBox.getItemText(areaOfLawListBox.getSelectedIndex()));
			}
		});
		publishButton.addClickHandler(new ClickHandler()  {
			@Override
			public void onClick(ClickEvent event)	{
				lawFirmService.publish(coveringLetterListBox.getItemText(coveringLetterListBox.getSelectedIndex()),
						new AsyncCallback<String>()	{
							public void onFailure(Throwable caught)	{
								errorLabelContainer.setText("Error Publishing "+ caught.toString());
							}
							public void onSuccess(String result)	{
								errorLabelContainer.setText("Publishing Successful");
							}
						});
			}
		});
		
		
	}
}
